package com.wanmait.schedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.schedule.entity.Course;
import com.wanmait.schedule.entity.CourseTeacher;
import com.wanmait.schedule.entity.Teacher;
import com.wanmait.schedule.mapper.CourseMapper;
import com.wanmait.schedule.mapper.CourseTeacherMapper;
import com.wanmait.schedule.mapper.TeacherMapper;
import com.wanmait.schedule.service.CourseTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 教师课程对应关系表 服务实现类
 * </p>
 *
 * @author wanmait
 * @since 2022-12-05
 */
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {

    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private CourseMapper courseMapper;
    @Resource
    private CourseTeacherMapper courseTeacherMapper;

    @Override
    public List<Teacher> listCourseByTeacher() {
        List<Teacher> teacherList = teacherMapper.selectList(null);
        for(Teacher teacher : teacherList){
            LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(CourseTeacher::getTeacherId,teacher.getId());
            List<CourseTeacher> courseTeachers = courseTeacherMapper.selectList(queryWrapper);
            List<Integer> courses = new ArrayList<>();
            for(CourseTeacher courseTeacher : courseTeachers){
                courses.add(courseTeacher.getCourseId());
            }
            teacher.setCourseIds(courses);
        }
        return teacherList;
    }

    @Override
    public PageInfo<Teacher> list(CourseTeacher courseTeacher, Integer pageNum) {
        List<Teacher> ts = new ArrayList<Teacher>();
        if(courseTeacher.getTeacherId()!=null || courseTeacher.getCourseId()!=null){
            LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(courseTeacher.getTeacherId()!=null,CourseTeacher::getTeacherId,courseTeacher.getTeacherId());
            queryWrapper.eq(courseTeacher.getCourseId()!=null,CourseTeacher::getCourseId,courseTeacher.getCourseId());
            List<CourseTeacher> cts = courseTeacherMapper.selectList(queryWrapper);
            if(cts.isEmpty()){
                List<Teacher> teacherl = new ArrayList<>();
                PageInfo<Teacher> pageInfo = new PageInfo<Teacher>(teacherl,9);
                return pageInfo;
            }
            Set<Integer> teacherList = new HashSet<>();
            for (CourseTeacher c:cts) {
                teacherList.add(c.getTeacherId());
            }
            Integer[] teachers = teacherList.toArray(new Integer[0]);
            PageHelper.startPage(pageNum,6);
            LambdaQueryWrapper<Teacher> teacherqueryWrapper = new LambdaQueryWrapper<>();
            teacherqueryWrapper.in(Teacher::getId,teachers);
            ts = teacherMapper.selectList(teacherqueryWrapper);
        }
        else {
            PageHelper.startPage(pageNum,6);
            ts = teacherMapper.selectList(null);
        }
        for(Teacher t : ts){
            LambdaQueryWrapper<CourseTeacher> tWrapper = new LambdaQueryWrapper<>();
            tWrapper.in(CourseTeacher::getTeacherId,t.getId());
            List<CourseTeacher> courseTeachers = courseTeacherMapper.selectList(tWrapper);
            List<Integer> courseList = new ArrayList<>();
            for(CourseTeacher ct : courseTeachers){
                courseList.add(ct.getCourseId());
            }
            t.setCourseIds(courseList);
        }
        if(pageNum==null){
            pageNum=1;
        }
        PageInfo<Teacher> pageInfo = new PageInfo<Teacher>(ts,9);
        return pageInfo;
    }

    @Override
    public List<Course> listByTeacherId(Integer id) {
        List<Course> courses = courseMapper.listByTeacherId(id);
        return courses;
    }

    @Override
    public void update(Integer id, Integer[] ids) {
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getTeacherId,id);
        List<CourseTeacher> courseTeachers = courseTeacherMapper.selectList(queryWrapper);
        for(CourseTeacher courseTeacher : courseTeachers){
            Boolean flag=true;
            for(Integer i : ids){
                if(i == courseTeacher.getCourseId()){
                    flag=false;
                    break;
                }
            }
            if(flag){
                LambdaQueryWrapper<CourseTeacher> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(CourseTeacher::getTeacherId,id);
                wrapper.eq(CourseTeacher::getCourseId,courseTeacher.getCourseId());
                courseTeacherMapper.delete(wrapper);
            }
        }
        for(Integer i : ids){
            Boolean flag=true;
            for(CourseTeacher courseTeacher : courseTeachers){
                if(i == courseTeacher.getCourseId()){
                    flag=false;
                    break;
                }
            }
            if(flag){
                CourseTeacher c = new CourseTeacher();
                c.setTeacherId(id);
                c.setCourseId(i);
                courseTeacherMapper.insert(c);
            }
        }
    }
}
